package yagwl.client.widget;

import com.google.gwt.user.client.ui.TextBox;

public class InputTextBox extends TextBox {
  private DataType dataType;
  // TODO p3 private String inlineHint;

  public InputTextBox(DataType dataType, int maxChar) {
    setMaxLength(maxChar);
    this.dataType = dataType;
  }

  public InputTextBox(DataType dataType, int maxChar, String width) {
    this(dataType, maxChar);
    setWidth(width);
  }

  @Override
  public String getText() {
    String t = super.getText().trim();
    return t.length() == 0 ? null : t;
  }
  
  public void clear() {
    setText("");
  }

  public boolean isValid() {
    switch (dataType) {
    case Integer:
      return isValidInteger(getText());
    case Decimal:
      return isValidDecimal(getText());
    case AlphaNumeric:
      return isValidAlphaNumeric(getText());
    case AlphaSpace:
      return isValidAlphaSpace(getText());
    case Email:
      return isValidEmail(getText());
    }
    return true;
  }
  
  private static native boolean isValidInteger(String text) /*-{
  var filter  = /^([0-9])+$/;
  return (filter.test(text));
 }-*/;

  private static native boolean isValidDecimal(String text) /*-{
  var filter  = /^([0-9\.])+$/;
  return (filter.test(text));
 }-*/;

  private static native boolean isValidAlphaNumeric(String text) /*-{
  var filter  = /^([a-zA-Z0-9])+$/;
  return (filter.test(text));
 }-*/;

  private static native boolean isValidAlphaSpace(String text) /*-{
  var filter  = /^([a-zA-Z\ ])+$/;
  return (filter.test(text));
 }-*/;

  private static native boolean isValidEmail(String text) /*-{
  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return (filter.test(text));
 }-*/;


  public enum DataType {
    Any, Integer, Decimal, AlphaNumeric, AlphaSpace, Email
  }

  //private class InputKeyPressHandler implements KeyPressHandler {
  //  @Override
  //  public void onKeyPress(KeyPressEvent event) {
  //      Logger.log(event.toDebugString());
  //       
  //    char charCode = event.getCharCode();
  //   System.out.println(charCode);
  //    Logger.log("" + ((int) charCode));
  //       switch (charCode) {
  // case KeyCodes.KEY_BACKSPACE://8
  // case KeyCodes.KEY_TAB://9
  // case KeyCodes.KEY_ENTER://13
  // case KeyCodes.KEY_PAGEUP: //33; also !
  // case KeyCodes.KEY_PAGEDOWN: //34; also "
  // case KeyCodes.KEY_END://35; also #
  // case KeyCodes.KEY_HOME://36; also $
  // case KeyCodes.KEY_LEFT://37; also %
  // case KeyCodes.KEY_UP: //38; also &
  // case KeyCodes.KEY_RIGHT: //39; also '
  // case KeyCodes.KEY_DOWN: //40; also (
  // case KeyCodes.KEY_DELETE://46; also .
  //   Logger.log("Allow - Navigation keys");
  //   return;
  // }
  // if (event.isAnyModifierKeyDown()) {
  //   Logger.log("Modifier key down");
  // }
  // // number pad keys
  // //if (charCode >= 96 && charCode <= 105)
  // //  charCode = (char) ('0' + charCode - 96);
  // if (dataType == DataType.Decimal || dataType == DataType.Integer
  //     || dataType == DataType.AlphaNumeric)
  //   if (charCode >= '0' && charCode <= '9') {
  //     Logger.log("Allow - Digit");
  //     return;
  //   }
  // if (dataType == DataType.AlphaNumeric || dataType == DataType.AlphaSpace)
  //   if ((charCode >= 'A' && charCode <= 'Z')
  //       || (charCode >= 'a' && charCode <= 'z')) {
  //     Logger.log("Allow - Alpha");
  //     return;
  //   }
  // if (dataType == DataType.AlphaSpace && charCode == ' ') {
  //   Logger.log("Allow - Space");
  //   return;
  // }
  // 
  // Logger.log("Disallow");
  // ((TextBox) event.getSource()).cancelKey();
  //   }
  // }

}
